package donajurema.model;

import java.util.Date;
import java.io.Serializable;

/**
 *  This class defines the basic "product" object.
 *  A product has the following features:
 *  	- Name		(default name and brand)
 *  	- Quantity	(specified amount)
 *  	- Unit 		(the product's unit of measurement)
 *  	- Shop 		(name of shop where item was bought)
 *  	- Date 		(date item was bought/added to cart)
 */
public class Product extends AbstractModelObject implements Comparable<Product>, Serializable
{
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private String	m_name;
	private Units	m_unit;
	private Integer	m_quantity;
	private String	m_shop;
	private Date	m_date;

	/** 
	 * Full Constructor used for products in History and in Loading
	 * @param name 
	 * @param quantity 
	 * @param unit 
	 * @param shop 
	 * @param date 
	 */
	public Product(String name, Integer quantity, Units unit, String shop, Date date)
	{
		this.setName(name);
		this.m_quantity = quantity;
		this.setUnit(unit);
		this.setShop(shop);
		this.m_date = date;
	}

	/**
	 *  Constructor used for products in Cart.
	 * @param name 
	 * @param quantity 
	 * @param unit 
	 */
	public Product(String name, Integer quantity, Units unit)
	{
		this(name, quantity, unit, "any", new Date());
	}

	/**
	 *  Constructor used for products in Database.
	 * @param name 
	 * @param unit 
	 */
	public Product(String name, Units unit)
	{
		this(name, 0, unit);
	}

	/**
	 *  Constructor to make this a Java Bean!!.
	 */
	public Product()
	{
		this("", Units.getDefault());
	}


	/**
	 *  Copy Constructor to clone object!!.
	 * @param p 
	 */
	public Product copy()
	{
		Product newProduct = new Product(this.getName(), this.getQuantity(), this.getUnit(), this.getShop(), this.getDate());
		return newProduct;
	}
	
	
	/**
	 * @return The full name of the product.
	 */
	public String getName()
	{
		return this.m_name;
	}
	
	public void setName(String name)
	{
		String oldValue = m_name;
		this.m_name = name;
		firePropertyChange("name", oldValue, m_name);
	}

	/**
	 * @return The numeric quantity.
	 */
	public Integer getQuantity()
	{
		return this.m_quantity;
	}
	
	public void setQuantity(Integer quantity)
	{
		Integer oldValue = m_quantity;
		this.m_quantity = quantity;
		firePropertyChange("quantity", oldValue, m_quantity);
	}

	public Units getUnit()
	{
		return this.m_unit;
	}
	
	public void setUnit(Units unit)
	{
		Units oldValue = m_unit;
		try
		{
			this.m_unit = unit; //Units.valueOf(unit.trim().toLowerCase());
		}
		catch (Exception e)
		{
			this.m_unit = Units.getDefault();
		}
		firePropertyChange("unit", oldValue, m_unit);
	}

	public String getShop()
	{
		return this.m_shop;
	}
	
	public void setShop(String shop)
	{
		String oldValue = m_shop;
		this.m_shop = shop;
		firePropertyChange("shop", oldValue, m_shop);
	}

	public Date getDate()
	{
		return this.m_date;
	}
	
	public void setDate(Date date)
	{
		
		Date oldValue = m_date;
		this.m_date = date;
		firePropertyChange("date", oldValue, m_date);
	}

	public void refreshDate()
	{
		this.m_date = new Date();
	}

	@Override
	public String toString()
	{
		if (this.m_quantity != -1)
		{
			return m_name + " (" + m_quantity.toString() + " " + m_unit.toString() + ")";
		}
		else
		{
			return m_name + " (" + m_unit.toString() + ")";
		}
	}
	
	@Override
	public int compareTo(Product anotherProduct)
	{
		String s1 = this.m_name.toLowerCase();
		String s2 = anotherProduct.m_name.toLowerCase();
	    return s1.compareTo(s2);
	}
}
